package net.edu_soft.ec.asset.service.impl;

import net.edu_soft.ec.asset.mapper.*;
import net.edu_soft.ec.asset.pojo.*;
import net.edu_soft.ec.asset.service.ManageService;
import net.edu_soft.ec.common.exception.ParameterException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.applet.Applet;
import java.util.ArrayList;
import java.util.List;

@Service
public class ManageServiceImpl implements ManageService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserAffMapper userAffService;

    @Autowired
    private FactoryMapper factoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ApplyMapper applyMapper;


    @Override
    public List<Factory> getAllFactory() {
        FactoryExample factoryExample=new FactoryExample();
        return factoryMapper.selectByExample(factoryExample);
    }

    @Override
    public List<Product> findByFactoryId(Integer id) {
        ProductExample productExample=new ProductExample();
        ProductExample.Criteria prcr=productExample.createCriteria();
        prcr.andFactoryIdEqualTo(id);
        return productMapper.selectByExample(productExample);
    }

    @Override
    public List<Comment> getComment(Integer id) {
        CommentExample commentExample=new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        //添加名字
        criteria.andSubjectIdEqualTo(id);
        return commentMapper.selectByExample(commentExample);
    }

    @Override
    public List<Factory> getUserAttention(Integer id) {
        List<Factory> factories = new ArrayList<>();
        UserAffExample setemal=new UserAffExample();
        UserAffExample.Criteria createCriteria = setemal.createCriteria();
        createCriteria.andUserInfoIdEqualTo(id);
        List<UserAff> userAffsInfomation=userAffService.selectByExample(setemal);
        if (userAffsInfomation.size()!=0) {
            //提取用户关注信息
            String[]  att=userAffsInfomation.get(0).getFactoryIds().split(",");
            for (String str : att) {
                factories.add(factoryMapper.selectByPrimaryKey(Integer.valueOf(str)));
            }
        }else{
            throw new ParameterException("未关注任何信息");
        }
        return factories;
    }

    @Override
    public List<Product> getUserProduct(Integer id) {
        List<Product> products = new ArrayList<>();
        UserAffExample setemal=new UserAffExample();

        UserAffExample.Criteria createCriteria = setemal.createCriteria();
        createCriteria.andUserInfoIdEqualTo(id);
        List<UserAff> userAffsInfomation=userAffService.selectByExample(setemal);
        if (userAffsInfomation.size()!=0) {
            //提取用户关注信息
            String[]  att=userAffsInfomation.get(0).getProductIds().split(",");
            for (String str : att) {
                products.add(productMapper.selectByPrimaryKey(Integer.valueOf(str)));
            }
        }else{
            throw new ParameterException("未关注任何信息");
        }
        return products;
    }

    @Override
    public List<Apply> getUserApply(Integer id) {
        ApplyExample applyExample=new ApplyExample();
        ApplyExample.Criteria criteria=applyExample.createCriteria();
        criteria.andUserNameEqualTo(String.valueOf(id));
        return applyMapper.selectByExample(applyExample);
    }



    @Override
    public List<Apply> getAllApply() {
        ApplyExample applyExample=new ApplyExample();
        return applyMapper.selectByExample(applyExample);
    }

    @Override
    public int updataApplyStatus(Integer id,Integer status) {
        ApplyExample applyExample=new ApplyExample();
        ApplyExample.Criteria apcr=applyExample.createCriteria();
        apcr.andApplyIdEqualTo(id);
        Apply apply=new Apply();
        apply.setStatus(status);
        return applyMapper.updateByExampleSelective(apply,applyExample);
    }

    @Override
    public List<Product> getProductList() {
        ProductExample productExample=new ProductExample();
        return productMapper.selectByExample(productExample);
    }


}
